﻿#include <QTime>
#include "CPdfRptRestOper.h"
#include "CGlobal.h"
#include "CPdfRptBase.h"

CPdfRptRestOper::CPdfRptRestOper(CPdfRptTool *pPdfRptTool, QObject *parent)
	: QObject(parent)
{
	m_pPdfRptTool = pPdfRptTool;
}

CPdfRptRestOper::~CPdfRptRestOper()
{

}

bool CPdfRptRestOper::drawSeSeOverviewForm(double &dStartRow, double &dStartCol, CAllPdfRptDataInfo &dataInfo, QPainter &painter, QPrinter &printer, QString &sErrMsg)
{
	if (m_pPdfRptTool == nullptr)
	{
		return false;
	}

	//判断是否需要换页画
	int nNeedRows = 6;
	m_pPdfRptTool->judgeExecAutoPageChg(dStartRow, dStartCol, nNeedRows, printer, sErrMsg);

	m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev24Bold, enPdfRptPenBlack);//设置字体和画笔等级
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 4, 2, A2T("声振温检测"), painter, printer, sErrMsg);//绘制表格附带文字-->表名称(声振温检测)

	m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev18, enPdfRptPenBlack);//设置字体和画笔等级

	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("检测日期"), painter, printer, sErrMsg);//绘制表格附带文字-->检测日期
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 3, 1, dataInfo.extraDataInfo.m_sDetTime, painter, printer, sErrMsg);//绘制表格附带文字-->检测日期

	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("报警项数"), painter, printer, sErrMsg);//绘制表格附带文字-->报警项数
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 3, 1, QString::number(dataInfo.extraDataInfo.m_nAlmCount), painter, printer, sErrMsg);//绘制表格附带文字-->报警项数

	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("检测开始时间"), painter, printer, sErrMsg);//绘制表格附带文字-->检测开始时间
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, dataInfo.extraDataInfo.m_sDetStartTime, painter, printer, sErrMsg);//绘制表格附带文字-->检测开始时间

	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("检测结束时间"), painter, printer, sErrMsg);//绘制表格附带文字-->检测结束时间
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, dataInfo.extraDataInfo.m_sDetEndTime, painter, printer, sErrMsg);//绘制表格附带文字-->检测结束时间

	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("备注"), painter, printer, sErrMsg);//绘制表格附带文字-->备注
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 3, 1, dataInfo.extraDataInfo.m_sComment, painter, printer, sErrMsg);//绘制表格附带文字-->备注

	return true;
}

bool CPdfRptRestOper::drawSeSeAllAlmDes(double &dStartRow, double &dStartCol, CAllPdfRptDataInfo &dataInfo, QPainter &painter, QPrinter &printer, QString &sErrMsg)
{
	if (dataInfo.extraDataInfo.m_vecFormSeSeAlmInfo.size() == 0)
	{
		return true;
	}

	//==========================按照排序规则重组===========================
	//先组装数据
	QMap<int, QVector<SPdfSeSeSglAlmInfo>> mapData;//key是通道号 QVector里面是该通道的所有报警数据
	for (int nIndex = 0; nIndex < dataInfo.extraDataInfo.m_vecFormSeSeAlmInfo.size(); nIndex++)
	{
		int nChannel = dataInfo.extraDataInfo.m_vecFormSeSeAlmInfo[nIndex].m_nChannelNo;//通道号
		if (mapData.contains(nChannel))
		{
			// 获取对应的 QVector
			QVector<SPdfSeSeSglAlmInfo> &vecAlmData = mapData[nChannel];
			vecAlmData.push_back(dataInfo.extraDataInfo.m_vecFormSeSeAlmInfo[nIndex]);
		}
		else
		{
			// 插入对应的 QVector
			QVector<SPdfSeSeSglAlmInfo> vecAlmData;
			vecAlmData.push_back(dataInfo.extraDataInfo.m_vecFormSeSeAlmInfo[nIndex]);
			mapData.insert(nChannel, vecAlmData);

		}
	}
	//然后每个通道里面的数据按照时间排序sortVectorByTime
	for (QMap<int, QVector<SPdfSeSeSglAlmInfo>>::iterator i = mapData.begin(); i != mapData.end(); i++)
	{
		sortVectorByTime(i.value());//内部按时间排序
	}

	//===================以上已重新排序完,接着绘制即可======================
	int nIndexCnt = 0;
	for (QMap<int, QVector<SPdfSeSeSglAlmInfo>>::iterator i = mapData.begin(); i != mapData.end(); i++)
	{
		QVector<SPdfSeSeSglAlmInfo> vecAlm = i.value();

		//遍历Vec容器
		for (int nIndex = 0; nIndex < vecAlm.size(); nIndex++)
		{
			if (0 == nIndexCnt)
			{
				printer.newPage();//添加新的一页 第一个声振温报警需强制换页开始显示
				dStartRow = 0;
				dStartCol = 0;

				//绘制详述表头
				drawSeSeAlmDesHeader(dStartRow, dStartCol, painter, printer, sErrMsg);
			}

			drawSeSeSingAlmDes(dStartRow, dStartCol, nIndexCnt, vecAlm[nIndex], painter, printer, sErrMsg);//绘制单个声振温报警详情(不包含表头)
			dStartRow += 1;//间隔一行再画下一组

			nIndexCnt++;
		}
	}

	return true;
}

bool CPdfRptRestOper::drawSeSeSingAlmDes(double &dStartRow, double &dStartCol, int nAlmIdex, SPdfSeSeSglAlmInfo &sglAlmInfo, QPainter &painter, QPrinter &printer, QString &sErrMsg)
{
	if (m_pPdfRptTool == nullptr)
	{
		return false;
	}

	//判断是否需要换页画
	int nNeedRows = 5;
	m_pPdfRptTool->judgeExecAutoPageChg(dStartRow, dStartCol, nNeedRows, printer, sErrMsg);

	m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev18, enPdfRptPenBlack);//设置字体和画笔等级

	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("报警序号"), painter, printer, sErrMsg);//绘制表格附带文字-->报警序号
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, QString::number(nAlmIdex + 1), painter, printer, sErrMsg);//绘制表格附带文字-->报警序号

	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("设备名称"), painter, printer, sErrMsg);//绘制表格附带文字-->设备名称
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, sglAlmInfo.m_sDevName, painter, printer, sErrMsg);//绘制表格附带文字-->设备名称 1

	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("通道名称"), painter, printer, sErrMsg);//绘制表格附带文字-->通道名称
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, sglAlmInfo.m_sChannelName, painter, printer, sErrMsg);//绘制表格附带文字-->通道名称

	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("报警时间"), painter, printer, sErrMsg);//绘制表格附带文字-->报警时间
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, sglAlmInfo.sAlmTime, painter, printer, sErrMsg);//绘制表格附带文字-->报警时间 2

	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("维度"), painter, printer, sErrMsg);//绘制表格附带文字-->维度
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, sglAlmInfo.sAccDirect, painter, printer, sErrMsg);//绘制表格附带文字-->维度

	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("温度诊断"), painter, printer, sErrMsg);//绘制表格附带文字-->温度诊断
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, sglAlmInfo.nTempAlm == 0 ? A2T("正常") : A2T("异常"), painter, printer, sErrMsg);//绘制表格附带文字-->温度诊断 3

	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("振动诊断"), painter, printer, sErrMsg);//绘制表格附带文字-->振动诊断
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, sglAlmInfo.nVibrationAlm == 0 ? A2T("正常") : A2T("异常"), painter, printer, sErrMsg);//绘制表格附带文字-->振动诊断

	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("声音诊断"), painter, printer, sErrMsg);//绘制表格附带文字-->声音诊断
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, sglAlmInfo.nVoiceAlm == 0 ? A2T("正常") : A2T("异常"), painter, printer, sErrMsg);//绘制表格附带文字-->声音诊断 4

	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("加速度"), painter, printer, sErrMsg);//绘制表格附带文字-->加速度
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, QString::number(sglAlmInfo.m_dRms, 'f', 2), painter, printer, sErrMsg);//绘制表格附带文字-->加速度 

	//温度值现场暂时没有,先不画
	//m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, A2T("温度值"), painter, printer, sErrMsg);//绘制表格附带文字-->温度值
	//m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 1, 1, QString::number(sglAlmInfo.m_dTempVal, 'f', 2), painter, printer, sErrMsg);//绘制表格附带文字-->温度值 5

	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 2, 1, A2T(""), painter, printer, sErrMsg);//5


	return true;
}

bool CPdfRptRestOper::drawSeSeAlmDesHeader(double &dStartRow, double &dStartCol, QPainter &painter, QPrinter &printer, QString &sErrMsg)
{
	if (m_pPdfRptTool == nullptr)
	{
		return false;
	}

	//判断是否需要换页画
	int nNeedRows = 2;
	m_pPdfRptTool->judgeExecAutoPageChg(dStartRow, dStartCol, nNeedRows, printer, sErrMsg);

	m_pPdfRptTool->setFontAndPenLevel(enPdfRptFontLev24Bold, enPdfRptPenBlack);//设置字体和画笔等级
	QString sTitle = A2T("%1").arg(A2T("声振温检测"));
	m_pPdfRptTool->drawFormAttachText(dStartRow, dStartCol, 4, 2, sTitle, painter, printer, sErrMsg);//绘制表格附带文字-->表名称(声振温检测)

}

bool CPdfRptRestOper::compareByTime(SPdfSeSeSglAlmInfo& sglAlmInfoOne, SPdfSeSeSglAlmInfo& sglAlmInfoTwo)
{
	// 将 QString 转换为 QTime，再进行比较
	QTime timeOne = QTime::fromString(sglAlmInfoOne.sAlmTime, "HH:mm:ss");
	QTime timeTwo = QTime::fromString(sglAlmInfoTwo.sAlmTime, "HH:mm:ss");

	return timeOne < timeTwo;// 根据时间升序排序
}

void CPdfRptRestOper::sortVectorByTime(QVector<SPdfSeSeSglAlmInfo>& vecData)
{
	qSort(vecData.begin(), vecData.end(), CPdfRptRestOper::compareByTime);
}
